﻿using System;
using System.Windows.Forms;
using ContractManager.Common.Class;
using ContractManager.Common.Service;
using ContractManager.Entity;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;

namespace ContractManager.Common.FormBase
{
    public partial class FormBase : XtraForm
    {
        #region "Define Variable"

        public RefreshHelper RefHelper;
        public GridCheckMarksSelection GcmSelection { get; set; }
        public RoleForms Role { get; set; }
        public Forms FuncForms { get; set; }

        public int AsSysMyCoYearId { get; set; }
        public int AsPerPersonId { get; set; }
        public bool SaveFormMaster { get; set; }

        public bool AddMode { get; set; }
        public bool EditMode { get; set; }
        public bool DeleteMode { get; set; }
        public bool GridMode { get; set; }

        public string FieldNameId { get; set; }
        public string StrCaption { get; set; }
        public int MasterId { get; set; }

        #endregion

        #region "Construction"

        public FormBase()
        {
            InitializeComponent();
            if (!String.IsNullOrEmpty(StrCaption))
            {
                this.Text = StrCaption;
            }
        }

        #endregion

        #region "Dispose Form"

        ~FormBase()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }

        #endregion

        #region "Public Method"

        public void ResetControls(Control cur)
        {
            foreach (Control c in cur.Controls)
            {
                var textEdit = c as TextEdit;
                var dateEdit = c as DateEdit;
                var memoEdit = c as MemoEdit;
                var calcEdit = c as CalcEdit;
                var lookUpEdit = c as LookUpEdit;
                var searchLookUpEdit = c as SearchLookUpEdit;

                if (textEdit != null)
                    ((TextEdit) c).EditValue = null;
                if (dateEdit != null)
                    ((DateEdit) c).EditValue = null;
                if (memoEdit != null)
                    ((MemoEdit) c).EditValue = null;
                if (calcEdit != null)
                    ((CalcEdit) c).EditValue = 0;
                if (lookUpEdit != null)
                    ((LookUpEdit) c).EditValue = null;
                if (searchLookUpEdit != null)
                    ((SearchLookUpEdit) c).EditValue = null;

                if (c.HasChildren)
                    ResetControls(c);
            }
        }

        public void ResetControls()
        {
            foreach (Control c in Controls)
            {
                var textEdit = c as TextEdit;
                var dateEdit = c as DateEdit;
                var memoEdit = c as MemoEdit;
                var calcEdit = c as CalcEdit;
                var lookUpEdit = c as LookUpEdit;
                var searchLookUpEdit = c as SearchLookUpEdit;
                var gridLookUpEdit = c as GridLookUpEdit;

                if (textEdit != null)
                    ((TextEdit) c).EditValue = null;
                if (dateEdit != null)
                    ((DateEdit) c).EditValue = null;
                if (memoEdit != null)
                    ((MemoEdit) c).EditValue = null;
                if (calcEdit != null)
                    ((CalcEdit) c).EditValue = 0;
                if (lookUpEdit != null)
                    ((LookUpEdit) c).EditValue = null;
                if (searchLookUpEdit != null)
                    ((SearchLookUpEdit) c).EditValue = null;
                if (gridLookUpEdit != null)
                    ((GridLookUpEdit) c).EditValue = null;

                if (c.HasChildren)
                    ResetControls(c);
            }
        }

        public void DisposeControls()
        {
            foreach (Control c in Controls)
            {
                c.Dispose();
            }
        }

        #endregion

        #region "Private Method"

        protected virtual void InitializeControls()
        {
            
        }

        protected virtual void LoadGrid()
        {
        }

        protected virtual void GetDataDetail()
        {
        }

        protected virtual void ClearRecord()
        {
        }

        protected void EnableRecord(bool yesNo)
        {
            btnNew.Visibility = yesNo ? BarItemVisibility.Always : BarItemVisibility.Never;
            btnEdit.Visibility = yesNo ? BarItemVisibility.Always : BarItemVisibility.Never;
            btnDelete.Visibility = yesNo ? BarItemVisibility.Always : BarItemVisibility.Never;
            btnView.Visibility = yesNo ? BarItemVisibility.Always : BarItemVisibility.Never;
            btnAttach.Visibility = yesNo ? BarItemVisibility.Always : BarItemVisibility.Never;
            btnSaveNew.Visibility = !yesNo ? BarItemVisibility.Always : BarItemVisibility.Never;
            btnSave.Visibility = !yesNo ? BarItemVisibility.Always : BarItemVisibility.Never;
            btnSaveClose.Visibility = !yesNo ? BarItemVisibility.Always : BarItemVisibility.Never;
        }


       protected void LockMenu(bool mnlock)
        {
            bool condition = ClsVariable.IsEditVoucher;
            btnNew.Enabled = Role.AllowAdd;
            btnEdit.Enabled = !mnlock && Role.AllowEdit && condition;
            btnDelete.Enabled = !mnlock && Role.AllowDelete && condition;
            btnView.Enabled = !mnlock && Role.AllowAccess;
            btnPrint.Enabled = Role.AllowPrint;
        }

        protected virtual void Add()
        {
        }

        protected virtual void Edit()
        {
        }

        protected virtual void Delete()
        {
        }

        protected virtual void View()
        {
        }

        protected virtual void Print()
        {
        }

        protected virtual void Attach()
        {
        }

        protected virtual void ReLoad()
        {
        }

        protected virtual void VerifyData()
        {
        }

        protected virtual void DeleteRecord()
        {
        }

        protected virtual void SaveRecord()
        {
        }

        protected virtual void GridDesign()
        {
        }

        private void DoClose()
        {
            Cursor = Cursors.WaitCursor;
            Close();
            Cursor = Cursors.Default;
        }

        #endregion

        #region "Form Event"

        private void XtraFormBase_FormClosed(object sender, FormClosedEventArgs e)
        {
            Dispose();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }

        private void FormBase_Shown(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            EnableRecord(GridMode);
            Cursor = Cursors.Default;
        }

        #endregion

        #region "Toolbar Event"
        private void tbMenu_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            switch (e.Item.Name)
            {
                case "btnNew":
                    Add();
                    break;
                case "btnEdit":
                    Edit();
                    break;
                case "btnDelete":
                    Delete();
                    break;
                case "btnView":
                    View();
                    break;
                case "btnRefresh":
                    ReLoad();
                    break;
                case "btnPrint":
                    Print();
                    break;
                case "btnAttach":
                    Attach();
                    break;
                case "btnSaveNew":
                    SaveRecord();
                    ClearRecord();
                    break;
                case "btnSave":
                    SaveRecord();
                    break;
                case "btnSaveClose":
                    SaveRecord();
                    DoClose();
                    break;
                case "btnDesign":
                    GridDesign();
                    break;
                case "btnClose":
                    DoClose();
                    break;
            }
            Cursor = Cursors.Default;
        }
        #endregion
    }
}